package com.tool.btn;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.SwingConstants;

import com.updateNew.MyIsif;
import org.come.Frame.MountJframe;
import org.come.Frame.MountSkillsJframe;
import org.come.Frame.OptionsJframe;
import org.come.Frame.TestpackJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.MountShouhuJpanel;
import org.come.Jpanel.MountSkillsJpanel;
import org.come.Jpanel.ZhuJpanel;
import org.come.Jpanel.mountJPanel;
import org.come.bean.RoleShow;
import org.come.entity.Mount;
import org.come.entity.RoleSummoning;
import org.come.mouslisten.ChangeMouseSymbolMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import com.tool.image.ImageMixDeal;
import com.tool.tcpimg.UIUtils;
import come.tool.Fighting.FightingMixDeal;
import come.tool.JDialog.TiShiUtil;
import come.tool.handle.HandleState;

public class MountPanelBtn extends MoBanBtn {

    private mountJPanel mountJPanel;
    private int caozuo;

    private mountJPanel untJPanel;
    private MountShouhuJpanel shouhuJpanel;

    public MountPanelBtn(String iconpath, int type, String text,
                         mountJPanel mountJPanel) {
        super(iconpath, type, UIUtils.COLOR_BTNPUTONG2);
        this.setText(text);
        setFont(UIUtils.TEXT_HY16);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        this.mountJPanel = mountJPanel;
    }

    public MountPanelBtn(final String iconpath, final int type, final String text, final mountJPanel untJPanel, final int caozuo, Color[] color) {
        super(iconpath, type, color);
        this.untJPanel = untJPanel;
        this.setText(text);
        this.setFont(UIUtils.TEXT_HY19);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.untJPanel = untJPanel;
        this.caozuo = caozuo;
    }

    public MountPanelBtn(final String iconpath, final int type, final String text, final mountJPanel untJPanel, final int caozuo) {
        super(iconpath, type, UIUtils.Black);
        this.setText(text);
        this.setFont(UIUtils.TEXT_HY16);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.untJPanel = untJPanel;
        this.caozuo = caozuo;
    }

    public MountPanelBtn(final String iconpath, final int type, final String text, final MountShouhuJpanel untJPanel, final int caozuo, Color[] color) {
        super(iconpath, type, color);
        this.setText(text);
        this.setFont(UIUtils.TEXT_HY19);
        this.setVerticalTextPosition(0);
        this.setHorizontalTextPosition(0);
        this.shouhuJpanel = untJPanel;
        this.caozuo = caozuo;
    }


    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        if (caozuo == 1 && FightingMixDeal.State == HandleState.USUAL) {
            rideOrReset();
        } else if (caozuo == 2 && FightingMixDeal.State == HandleState.USUAL) {
            rideOrReset();
        } else if ("清除".equals(this.getText()) && FightingMixDeal.State == HandleState.USUAL) {
            clearControlPet();
        } else if (caozuo == 3 && FightingMixDeal.State == HandleState.USUAL) {
            controlPet();
        } else if (caozuo == 4 && FightingMixDeal.State == HandleState.USUAL) {
            feedMount();
        } else if (caozuo == 5 && FightingMixDeal.State == HandleState.USUAL) {
            mountSkill();
        } else if (caozuo == 6 && FightingMixDeal.State == HandleState.USUAL) {
            /**判断是否解锁*/
            if (Util.isCanBuyOrno()) {
                return;
            }
            releaseMount();
        } else if (this.caozuo == 7 && FightingMixDeal.State == 0) {
            String sendmes = Agreement.getAgreement().MountAgreement();
            SendMessageUntil.toServer(sendmes);
            try {
                String style = MyIsif.getStyle();
                if ("水墨UI".equals(style)) {
                    untJPanel.getShouhu().get(0).setIcons(CutButtonImage.cuts("inkImg/Client/选中按钮.png"));
                    untJPanel.getShouhu().get(1).setIcons(CutButtonImage.cuts("inkImg/Client/未选中按钮.png"));
                } else {
                    untJPanel.getShouhu().get(0).setIcons(CutButtonImage.cuts("inkImg/Client/选中按钮.png"));
                    untJPanel.getShouhu().get(1).setIcons(CutButtonImage.cuts("inkImg/hongmu/未选中按钮(1).png"));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            FormsManagement.HideForm(2259);
        } else if (this.caozuo == 8 && FightingMixDeal.State == 0) {
            try {
                final String sendmes = Agreement.getAgreement().shouhuAgreement("open");
                SendMessageUntil.toServer(sendmes);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            FormsManagement.HideForm(7);
        } else if (this.caozuo == 9 && FightingMixDeal.State == 0) {
            String sendmes = Agreement.getAgreement().MountAgreement();
            SendMessageUntil.toServer(sendmes);
            try {
                String style = MyIsif.getStyle();
                if ("水墨UI".equals(style)) {
                    shouhuJpanel.getShouhu().get(0).setIcons(CutButtonImage.cuts("inkImg/Client/选中按钮.png"));
                    shouhuJpanel.getShouhu().get(1).setIcons(CutButtonImage.cuts("inkImg/Client/未选中按钮.png"));
                } else {
                    shouhuJpanel.getShouhu().get(0).setIcons(CutButtonImage.cuts("inkImg/Client/选中按钮.png"));
                    shouhuJpanel.getShouhu().get(1).setIcons(CutButtonImage.cuts("inkImg/hongmu/未选中按钮(1).png"));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            FormsManagement.HideForm(2259);
        } else if (this.caozuo == 10 && FightingMixDeal.State == 0) {
            if (!FormsManagement.getframe(2259).isVisible()) {
                // 向服务器发送消息请求坐骑列表
                FormsManagement.showForm(2259);
                //  String sendmes = Agreement.getAgreement().MountAgreement();
                //  SendMessageUntil.toServer(sendmes);
                Music.addyinxiao("开关窗口.mp3");
            }
            try {
                String style = MyIsif.getStyle();
                if ("水墨UI".equals(style)) {
                    shouhuJpanel.getShouhu().get(0).setIcons(CutButtonImage.cuts("inkImg/Client/未选中按钮.png"));
                    shouhuJpanel.getShouhu().get(1).setIcons(CutButtonImage.cuts("inkImg/Client/选中按钮.png"));
                } else {
                    shouhuJpanel.getShouhu().get(0).setIcons(CutButtonImage.cuts("inkImg/hongmu/未选中按钮(1).png"));
                    shouhuJpanel.getShouhu().get(1).setIcons(CutButtonImage.cuts("inkImg/hongmu/未选中按钮(1).png"));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            FormsManagement.HideForm(7);
        }

    }

    /**
     * 骑乘/清除管制
     */
    public void clearControlPet() {

        if (MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedValue() == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("请选择坐骑！");
            return;
        }
        int index1 = MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex();
        Mount mount = ZhuJpanel.getListMount().get(index1);

        // 判断是取消管制还是 管制
        mount.setSid(null);
        mount.setOthrersid(null);
        mount.setSid3(null);
        mount.setSid4(null);
        mount.setSid5(null);

        String sendmes = Agreement.getAgreement().changeMountValue(GsonUtil.getGsonUtil().getgson().toJson(mount));
        SendMessageUntil.toServer(sendmes);
        sendmes = Agreement.getAgreement().MountAgreement();
        SendMessageUntil.toServer(sendmes);
    }

    /**
     * 骑乘/休息
     */
    public void rideOrReset() {
        mountJPanel mountJPanel = MountJframe.getMountjframe().getMountjpanel();
        RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
        if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
            ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以乘骑坐骑！");
            return;
        }
        if (roleShow.getMapid().intValue() == 1732 || roleShow.getMapid().intValue() == 1761) {
            ZhuFrame.getZhuJpanel().addPrompt2("当前地图不可以乘骑坐骑");
            return;
        }
//		if(roleShow.getFly_id()>0) {
//			ZhuFrame.getZhuJpanel().addPrompt2("飞行状态不可以乘骑坐骑");
//			return;
//		}
        if (mountJPanel.getListmount().getSelectedValue() != null) {
            int index = mountJPanel.getListmount().getSelectedIndex();
            if ("骑乘".equals(mountJPanel.getBtnRiding().getText())) {
                if (roleShow.getMount_id() != 0) {
                    mountJPanel.getModelmount().set(index, "( * ) " + mountJPanel.getModelmount().get(index));
                    for (int i = 0; i < ZhuJpanel.getListMount().size(); i++) {
                        if (ZhuJpanel.getListMount().get(i).getMountid() == roleShow.getMount_id()) {
                            mountJPanel.getModelmount().set(i, ZhuJpanel.getListMount().get(i).getMountname());
                            break;
                        }
                    }
                    roleShow.setMount_id(ZhuJpanel.getListMount().get(index).getMountid());
                } else {
                    mountJPanel.getModelmount().set(index, "( * ) " + mountJPanel.getModelmount().get(index));
                    roleShow.setMount_id(ZhuJpanel.getListMount().get(index).getMountid());
                }
                ExpIncreaseUntil.showMountValue(ZhuJpanel.getListMount().get(index));
                ImageMixDeal.userimg.changeskin(null);
                String mes = Agreement.getAgreement().rolechangeAgreement("M" + roleShow.getMount_id() + "=" + ZhuJpanel.getListMount().get(index).getMid());
                SendMessageUntil.toServer(mes);
                MountJframe.getMountjframe().getMountjpanel().getBtnRiding().setText("休息");
            } else if ("休息".equals(MountJframe.getMountjframe().getMountjpanel().getBtnRiding().getText())) {
                MountJframe.getMountjframe().getMountjpanel().getModelmount().set(index, ZhuJpanel.getListMount().get(index).getMountname());
                roleShow.setMount_id(0);
                ImageMixDeal.userimg.changeskin(null);
                String mes = Agreement.getAgreement().rolechangeAgreement("M");
                SendMessageUntil.toServer(mes);
                MountJframe.getMountjframe().getMountjpanel().getBtnRiding().setText("骑乘");
            }
        } else {
            ZhuFrame.getZhuJpanel().addPrompt2("请选择您要骑乘/休息的坐骑！");
        }
    }

    /**
     * 管制
     */
    public void controlPet() {

        if (MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedValue() == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("请选择坐骑！");
            return;
        } else if (org.come.Jpanel.mountJPanel.getListpet().getSelectedIndex() == -1) {
            ZhuFrame.getZhuJpanel().addPrompt2("请选择你要管制的召唤兽！");
            return;
        }
        int index1 = MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex();
        int index2 = org.come.Jpanel.mountJPanel.getListpet().getSelectedIndex();
        RoleSummoning pet = UserMessUntil.getPetListTable().get(index2);
        Mount mount = ZhuJpanel.getListMount().get(index1);
        Mount mount2 = ZhuJpanel.getPetMount(pet.getSid());
        if (mount2 != null && mount != mount2) {
            ZhuFrame.getZhuJpanel().addPrompt2("该召唤兽已被其他坐骑管制");
            return;
        }
        // 判断是取消管制还是 管制
        int type = 0;
        if ((mount.getSid() != null) && (pet.getSid().compareTo(mount.getSid()) == 0)) {
            type = -1;
            mount.setSid(null);
        } else if ((mount.getOthrersid() != null) && (pet.getSid().compareTo(mount.getOthrersid()) == 0)) {
            type = -2;
            mount.setOthrersid(null);
        } else if ((mount.getSid3() != null) && (pet.getSid().compareTo(mount.getSid3()) == 0)) {
            type = -3;
            mount.setSid3(null);
        } else if ((mount.getSid4() != null) && (pet.getSid().compareTo(mount.getSid4()) == 0)) {
            type = -4;
            mount.setSid4(null);
        } else if ((mount.getSid5() != null) && (pet.getSid().compareTo(mount.getSid5()) == 0)) {
            type = -5;
            mount.setSid5(null);
        } else if (mount.getSid() == null) {
            mount.setSid(pet.getSid());
            type = 1;
        } else if (mount.getOthrersid() == null) {
            mount.setOthrersid(pet.getSid());
            type = 2;
        } else if (mount.getSid3() == null) {
            mount.setSid3(pet.getSid());
            type = 3;
        } else if (mount.getSid4() == null) {
            mount.setSid4(pet.getSid());
            type = 4;
        } else if ((mount.getMountlvl().intValue() > 100) && (mount.getSid5() == null)) {
            mount.setSid5(pet.getSid());
            type = 5;
        }
        if (type == 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("管制召唤兽上限");
            return;
        } else if (type < 0) {
            ChangeMouseSymbolMouslisten.clearPropertie(pet);
            Vector<String> list = MountJframe.getMountjframe().getMountjpanel().getVerVectors().get(index2);
            list.set(1, "");
        } else {
            ChangeMouseSymbolMouslisten.addProperties(pet, mount);
            Vector<String> list = MountJframe.getMountjframe().getMountjpanel().getVerVectors().get(index2);
            list.set(1, mount.getMountname());
        }
        String sendmes = Agreement.getAgreement().changeMountValue(GsonUtil.getGsonUtil().getgson().toJson(mount));
        SendMessageUntil.toServer(sendmes);
    }

    /**
     * 喂养
     */
    public void feedMount() {
        if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
            ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以喂养！");
            return;
        }
        // 将使用物品的状态改为坐骑喂养状态
        ZhuJpanel.setUseGoodsType(2);
        TestpackJframe.getTestpackJframe().setLocation(500, 80);
        FormsManagement.upgradForm(2);

    }

    /**
     * 技能
     */
    public void mountSkill() {

        // 先判断是否有坐骑
        if (ZhuJpanel.getListMount() != null) {

            // 判断是否有选中坐骑
            if (MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex() != -1) {

                int index = MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex();
                //将召唤兽技能熟练度展示出来
                MountSkillsJframe.getMountSkillsJframe().getMountSkillsJpanel().getLabProficiency().setText(ZhuJpanel.getListMount().get(index).getProficiency() + "");
                // 先判断这只召唤兽是否有技能
                if (ZhuJpanel.getListMount().get(index).getMountskill().size() != 0) {

                    refreshMountSkills(index);

                } else {

                    MountSkillsJframe.getMountSkillsJframe()
                            .getMountSkillsJpanel().getModelskilleffect()
                            .removeAllElements();
                    MountSkillsJframe.getMountSkillsJframe()
                            .getMountSkillsJpanel().getModelmountskill()
                            .removeAllElements();

                    // 提示信息
                    ZhuFrame.getZhuJpanel().addPrompt2("该坐骑还没有技能，赶紧去学习吧！");

                }

                FormsManagement.showForm(20);
            } else {

                // 提示信息
                ZhuFrame.getZhuJpanel().addPrompt2("您还没有坐骑，赶快去获取吧！");

            }
        }

    }

    /**
     * 放生
     */
    public void releaseMount() {
        //先判断是否有选择坐骑
        if (MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex() == -1) {
            ZhuFrame.getZhuJpanel().addPrompt("请选择您要放生的坐骑！");
            return;
        }
        int index = MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex();
        Mount mount = ZhuJpanel.getListMount().get(index);
        //判断这只坐骑是否有骑乘
        if (ImageMixDeal.userimg.getRoleShow().getMount_id() != 0 && ImageMixDeal.userimg.getRoleShow().getMount_id() == mount.getMountid()) {
            ZhuFrame.getZhuJpanel().addPrompt("您的坐骑还被您骑着呢！");
            return;
        }
        //判断这只坐骑是否有管制召唤兽
        if (mount.getSid() != null || mount.getOthrersid() != null || mount.getSid3() != null || mount.getSid4() != null || mount.getSid5() != null) {
            ZhuFrame.getZhuJpanel().addPrompt("您的坐骑管制着召唤兽呢！");
            return;
        }
        // 出现放生的界面
        OptionsJframe.getOptionsJframe().getOptionsJpanel().
                showBox(TiShiUtil.Release, mount, "#W确定要将坐骑:#G" + mount.getMountname() + "#W放生吗?");
    }

    // 刷新坐骑技能的方法
    public static void refreshMountSkills(int index) {
        MountSkillsJpanel jpanel = MountSkillsJframe.getMountSkillsJframe().getMountSkillsJpanel();
        jpanel.getModelmountskill().removeAllElements();
        jpanel.getListmountskill().removeAll();
        // 将技能展示出来
        for (int i = 0; i < ZhuJpanel.getListMount().get(index).getMountskill().size(); i++) {
            jpanel.getModelmountskill().add(i, ZhuJpanel.getListMount().get(index).getMountskill().get(i).getSkillname());
        }
        //将第一个技能的技能效果展示出来
        jpanel.getListmountskill().setSelectedIndex(0);
        if (Util.calculateAddition(ZhuJpanel.getListMount().get(index), ZhuJpanel.getListMount().get(index).getMountskill().get(0).getSkillname()) != null) {
            String[] mountaut = Util.calculateAddition(ZhuJpanel.getListMount().get(index), ZhuJpanel.getListMount().get(index).getMountskill().get(0).getSkillname()).split("\\|");
            jpanel.getModelskilleffect().removeAllElements();
            // 将技能属性一个个展示出来
            for (int j = 0; j < mountaut.length; j++) {
                jpanel.getModelskilleffect().add(j, Util.changeToPercentage(mountaut[j]));
            }
        }
    }
}
